﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using IrisGenerator.Framework.Reflection;

namespace IrisGenerator.Framework.UI.Controls
{
    public class BindableToolStripItemCollection<T>:List<ToolStripItem>,IBindableControl
    {
        public BindableToolStripItemCollection()
            : base()
        {
            
        }

        
        #region IBindableControl Members

        public void LoadSource()
        {
            if (this.SourceMethod != null)
                this.DataSource = GetDataSourceFromMethod();

            IList items = this.DataSource as IList;
            if(items!=null)
            {
                foreach (object obj in items)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    if (!string.IsNullOrEmpty(this.TextDataProperty))
                    {
                        item.Text = Property.GetPropertyValue(obj, this.TextDataProperty) as string;
                    }
                        
                }
            }
        }

        public string TextDataProperty { get; set; }
        public string TagDataProperty { get; set; }

        private object GetDataSourceFromMethod()
        {
            bool result;
            object tmp = SourceMethod.Invoke(this, out result);
            return tmp;
        }

        public IrisGenerator.Framework.Reflection.MethodCall SourceMethod
        {
            get;
            set;
        }

        public object DataSource { get; set; }

        #endregion
    }
}
